package com.example.oppotest.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.hash.Hashing;
import com.google.common.io.BaseEncoding;
import com.omni.ads.baseconfig.OmniAds;
import com.omni.ads.baseconfig.ResultDto;
import com.omni.ads.examples.communal.V3CommunalConfigGetList;
import com.omni.ads.exception.OmniAdsResponseException;
import com.omni.ads.model.adscommunal.AdsConfigReq;
import com.omni.ads.model.adscommunal.AdsConfigResp;

import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @Author wyk
 * @Date: 2025/2/16 21:37
 */
public class oppoUtils {

    public static final long ownerId = 1000342639; // 用户帐号id,为营销平台的广告主ID

//    public static final long ownerId = 1000507436; // 用户帐号id,为营销平台的广告主ID

    public static final String oppoApiID = "532bb0a9140c4d6f95373fe6f7aeba7c"; // API授权接入方唯一身份标识

    public static final String oppoApiKey = "f88455466d404759beb9b9361874d947"; // 开通API授权后获得的私钥

    public static final String testUrl = "https://sapi-ads-test.wanyol.com"; // 测试环境域名

    public static final String productUrl = "https://sapi.ads.heytapmobi.com/"; // 上线环境域名

    public static String generateToken() {
        int timestamp = unixTime();

        String sign = Hashing.sha1().hashString(oppoApiID + oppoApiKey + timestamp, Charset.defaultCharset()).toString();

        String token = BaseEncoding.base64().encode((ownerId + "," + oppoApiID + "," + timestamp + "," + sign).getBytes());

        return token;
    }

    public static int unixTime() {
        BigDecimal num = new BigDecimal(System.currentTimeMillis() / 1000L);

        return num.intValue();
    }

    /**
     * SDK初始化
     *
     */
    public static OmniAds getOmniAPI() {
        OmniAds omniAds = OmniAds.getInstance(ownerId, oppoApiID, oppoApiKey);
        omniAds.useSandbox();
        omniAds.setDebug(true);
        return omniAds;
    }

    public static ResultDto<List<AdsConfigResp>> getConfigList() throws Exception {
        OmniAds omniAds = getOmniAPI();
        AdsConfigReq data = new AdsConfigReq();
        data.setType("EXTENSION_TYPE");
        ResultDto<List<AdsConfigResp>> res = omniAds.adsCommunal().v3CommunalConfigGetList(data);
        return res;
    }


}
